linq to sql using foreign keys returning iqueryable(of myEntity]

Posted by Gern Blandston on Stack Overflow See other posts from Stack Overflow or by Gern Blandston
Published on 2010-05-04T19:56:13Z Indexed on 2010/05/11 4:34 UTC
Read the original article Hit count: 286

Filed under:
|
|

I'm trying to use Linq to SQL to return an IQueryable(of Project) when using foreign key relationships. Using the below schema, I want to be able to pass in a UserId and get all the projects created for the company the user is associated with.

DB tables:

Projects
  Projid
  ProjCreator FK (UserId from UserInfo table)
  Companyid FK (CompanyID from Companies table)

UserInfo
  UserID PK
  Companyid FK

Companies
 CompanyId PK
 Description

I can get the iqueryable(of project) when simply getting the ProjectCreator with this:

Return (From p In db.Projects _
 Where p.ProjectCreator = Me.UserId)

But I'm having trouble getting the syntax to get a iqueryable(of projects) when using foreign keys. Below gives me an IQueryable(of anonymous) but I can't seem to convince it to give me an IQueryable(of project) even if I try to cast it:

  Dim retval = (From p In db.Projects _
    Join c In db.Companies On p.CompanyId Equals c.CompanyId _
     Join u In db.UserInfos On u.CompanyId Equals c.CompanyId _
      Where u.Login = UserId)

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-sql